const user = require("../sql/user");
const sql = require("../sql/index");
const uuid = require("uuid");

exports.register = function(msg){
    return (req, res, next)=>{
        sql.get(user, {username: req.body.username}, data=>{
            if(data.length < 1){
                sql.add(user, {
                    userid: "user-" + uuid.v1(),
                    username: req.body.username,
                    password: req.body.password,
                    isLogin: 0,
                    regTime: Date.now()
                }, ()=>{
                    res.send(JSON.stringify({
                        code:1,
                        msg:"注册成功"
                    }));
                }, err=>{
                    res.send(JSON.stringify({
                        code:2,
                        msg:"注册失败，数据库出错",
                        err
                    }));
                })
            }else{
                res.send(JSON.stringify({
                    code:0,
                    msg:"注册失败，用户名重复"
                }));
            }
        })
    };
}
